package com.masterclient.app.modules.pickgoods;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselibrary.bean.order.ConfirmOrderData;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.modules.pickgoods.contract.PickGoodsContract;
import com.masterclient.app.modules.pickgoods.contract.PickGoodsPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：hxm
 * 创建时间：2020/10/20 14:05
 * 功能说明：
 */
@Route(path = AroutePath.PICK_GOODS_ACTIVITY)
public class PickGoodsActivity extends BaseActivity<PickGoodsPresenter> implements PickGoodsContract.View {

    @BindView(R.id.tv_submit)
    TextView tvSubmit;

    @Autowired(name = "position")
    int position;
    PickGoodsListFragment pickGoodsListFragment;
    private boolean mHasZeroNum = false;
    private boolean isCanClick = false;
    String pickGoodsBuyProducts;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_goods);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);

        pickGoodsListFragment = PickGoodsListFragment.newInstance();
        pickGoodsListFragment.setSelectPickGoodsListener((showEmpty,buyProduct, hasZeroNum) -> {
            mHasZeroNum = hasZeroNum;
            pickGoodsBuyProducts = buyProduct;
            if (!TextUtils.isEmpty(buyProduct)){
                isCanClick = true;
                tvSubmit.setBackgroundResource(R.drawable.bg_pick_goods_selected);
            }else {
                isCanClick = false;
                tvSubmit.setBackgroundResource(R.drawable.bg_pick_goods_unselect);
            }
            tvSubmit.setVisibility(showEmpty?View.INVISIBLE:View.VISIBLE);
        });
        getSupportFragmentManager().beginTransaction().add(R.id.content_view,pickGoodsListFragment).commit();

    }

    @OnClick({R.id.rl_back,R.id.tv_look_order,R.id.tv_submit})
    void onClick(View v){
        switch (v.getId()){
            case R.id.rl_back:
                finish();
                break;
            case R.id.tv_look_order:
                ARouter.getInstance().build(AroutePath.STORAGE_ORDER_LIST_ACTIVITY).navigation();
                break;
            case R.id.tv_submit:
                if (isCanClick){
                        if (mHasZeroNum){
                            ToastUtil.showToast("提货数量不能为0");
                            return;
                        }
                        mPresenter.confirm_order(pickGoodsBuyProducts, "2", "","6","","0");
                }
                break;
        }
    }

    @Override
    public void confirm_order_success(ConfirmOrderData data) {
            ARouter.getInstance().build(AroutePath.ORDER_SUBMIT_ACTIVITY)
                    .withString("buy_type", "2").
                    withString("buy_product", pickGoodsBuyProducts).
                    withString("saleType", "6").
                    withSerializable("confirm_order_data", data).navigation();
    }

    @Override
    public void confirm_order_failed(String msg) {
        ToastUtil.showToast(msg);
    }
}
